﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;
using Oliving.Edge.Model.Dto.WY;
using Oliving.Edge.IRepository.WY;
using Oliving.Edge.Model.Models.XLT;

namespace Oliving.Edge.Repository.WY
{
    /// <summary>
    /// WY_ProjectRepository
    /// </summary>
    public class WY_ProjectRepository : BaseRepository<WY_Project>, IWY_ProjectRepository
    {
        public WY_ProjectRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }






        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="pageIndex" > 页码（下标0）</param>
        /// <param name="pageSize" > 页大小</param>
        /// <param name="sortField" > 排序字段</param>
        /// <param name="sortOrder" > 排序顺序（asc/desc）</param>
        /// <returns></returns>
        public async Task<PageModel<WY_ProjectPageDto>> QueryPageList(
                Expression<Func<WY_Project, bool>> whereExpression,
                int pageIndex = 1, int pageSize = 20,
                string sortField = null, string sortOrder = null
                )
        {


            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion

            var totalCount = 0;

            var pageJoin = Db.Queryable<WY_Project, WY_Company>((_WY_Project, _WY_Company) => new object[]
                  {
                    JoinType.Left,_WY_Project.ComID==_WY_Company.Id
                   })
                .WhereIF(whereExpression != null, whereExpression)
                .Select((_WY_Project, _WY_Company) => new WY_ProjectPageDto
                {
                    ComName = _WY_Company.CompanyName,

                    ProName = _WY_Project.ProName,
                    Id = _WY_Project.Id,
                    PartyA = _WY_Project.PartyA,
                    PartyAUserName = _WY_Project.PartyAUserName,
                    PartyAPhone = _WY_Project.PartyAPhone,
                    ProUser = _WY_Project.ProUser
                })
                .ToPageList(pageIndex, pageSize, ref totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();

            return new PageModel<WY_ProjectPageDto>()
            {
                dataCount = totalCount,
                pageCount = pageCount,
                page = pageIndex,
                PageSize = pageSize,
                data = pageJoin
            };




        }







    }
}

